﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package zasp.Util {

    public class SumSampler implements Sampler {

        private var cutoffSum_:Number;
        private var runningSum_:Number;
        private var samples_:CircularBuffer;

        public function SumSampler(_arg1:Number){
            this.samples_ = new CircularBuffer(0);
            super();
            this.cutoffSum_ = _arg1;
            this.runningSum_ = 0;
        }
        public static function test():void{
            var _local1:SumSampler = new SumSampler(50);
            if (0 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 0)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(10.1);
            _local1.sample(15.2);
            _local1.sample(20.3);
            if (3 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 45.6)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(5);
            if (4 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 50.6)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(0);
            _local1.sample(0);
            _local1.sample(0);
            if (7 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 50.6)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(3.3);
            if (8 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 53.9)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(7);
            if (8 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 50.8)) > 0.01){
                throw (new Error(""));
            };
            if (15.2 != _local1.sampleAt(0)){
                throw (new Error(""));
            };
            if (20.3 != _local1.sampleAt(1)){
                throw (new Error(""));
            };
            if (5 != _local1.sampleAt(2)){
                throw (new Error(""));
            };
            if (0 != _local1.sampleAt(3)){
                throw (new Error(""));
            };
            if (0 != _local1.sampleAt(4)){
                throw (new Error(""));
            };
            if (0 != _local1.sampleAt(5)){
                throw (new Error(""));
            };
            if (3.3 != _local1.sampleAt(6)){
                throw (new Error(""));
            };
            if (7 != _local1.sampleAt(7)){
                throw (new Error(""));
            };
            _local1.sample(123.4);
            if (1 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 123.4)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(49);
            if (2 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 172.4)) > 0.01){
                throw (new Error(""));
            };
            _local1.sample(1.1);
            if (2 != _local1.length){
                throw (new Error(""));
            };
            if (Math.abs((_local1.value - 50.1)) > 0.01){
                throw (new Error(""));
            };
        }

        public function sample(_arg1:Number):void{
            var _local2:Number;
            if (this.samples_.length >= this.samples_.capacity){
                this.samples_.capacity = (1 + (1.3 * this.samples_.capacity));
            };
            this.samples_.push(_arg1);
            this.runningSum_ = (this.runningSum_ + _arg1);
            while (this.samples_.length > 0) {
                _local2 = (this.samples_.elementAt(0) as Number);
                if ((this.runningSum_ - _local2) < this.cutoffSum_){
                    break;
                };
                this.samples_.pop();
                this.runningSum_ = (this.runningSum_ - _local2);
            };
        }
        public function get value():Number{
            return (this.runningSum_);
        }
        public function get length():int{
            return (this.samples_.length);
        }
        public function sampleAt(_arg1:int):Number{
            return ((this.samples_.elementAt(_arg1) as Number));
        }

    }
}//package zasp.Util 
